package com.aiwhose.aiwhosesecurity.utils;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;

@Component
public class LineCaptcha {


    public LineCaptcha() {

    }

    public String getImgCode(String phone,RedisUtil redisUtil) throws FileNotFoundException {
        if (redisUtil == null) {
            redisUtil = new RedisUtil();
        }
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 4);
        String imageBase64 = captcha.getImageBase64();
        String code = captcha.getCode();
        redisUtil.set("IMG_VERIFY_" + phone, code, 5 * 60);
        return imageBase64;
    }

    public Boolean checkVerify(String phone, String code,RedisUtil redisUtil) {
        if (redisUtil == null) {
            redisUtil = new RedisUtil();
        }
        String string = (String) redisUtil.get("IMG_VERIFY_"+ phone);
        if (StringUtil.isNullOrEmpty(string)) {
            return false;
        }
        return string.equalsIgnoreCase(code);
    }


}
